﻿@page
@model WXWorkSvc.Common.Pages.Logger.ListModel
@{
    ViewData["Title"] = "日志";
    var first = -(Model.Items.FirstOrDefault()?.Id ?? 0);
    var last = Model.Items.LastOrDefault()?.Id ?? 0;
}

<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark border-bottom box-shadow mb-3 fixed-top">

    <div class="container-fluid">
        <a class="navbar-brand" href="@TempData.Peek("ReturnUrl")">返回</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                @if (Model.HasPrevious)
                {
                    <li class="nav-item">
                        <a class="nav-link" asp-page="./List"   asp-route-id="@first" asp-route-level="@Model.Level" asp-route-SearchText="@Model.SearchText">前一页</a>
                    </li>
                }
                @if (Model.HasNext)
                {
                    <li class="nav-item">
                        <a class="nav-link" asp-page="./List"  asp-route-id="@last" asp-route-level="@Model.Level" asp-route-SearchText="@Model.SearchText">后一页</a>
                    </li>
                }
                <li class="nav-item">
                    <a class="nav-link" asp-page="./List" asp-route-level="@Model.Level" asp-route-SearchText="@Model.SearchText">刷新</a>
                </li>
            </ul>
            <form class="d-flex" role="search" id="Form1" method="get">
                <select nameid="FormChange" class="form-select me-1" asp-for="Level" asp-items="Html.GetEnumSelectList<LogLevel>()" style="width:150px"></select>
                <input nameid="FormChange" class="form-control me-1" type="search" placeholder="全文搜索" style="width:300px" asp-for="SearchText">
            </form>
        </div>
    </div>
</nav>

<table class="table table-striped table-sm table-hover">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].Id)
            </th>
            <th style="width:155px">
                @Html.DisplayNameFor(model => model.Items[0].Dt)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].LogLevel)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].ShortMsg)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Items)
        {
            string trclass;
            switch (item.LogLevel)
            {
                case LogLevel.Error:
                    trclass = "table-danger";
                    break;
                case LogLevel.Warning:
                    trclass = "table-warning";
                    break;
                default:
                    trclass = "table-info";
                    break;
            }

            <tr class="@trclass">
                <td> <a asp-page="./Detail" asp-route-id="@item.Id">@item.Id</a></td>
                <td>@item.Dt</td>
                <td>@item.LogLevel</td>
                <td onclick="SetSearch('@item.Name')">@item.Name</td>
                <td style="word-break:break-all; word-wrap:break-all;"><brs>@item.ShortMsg</brs></td>
            </tr>

        }
    </tbody>
</table>

@section scripts
{
    <script>
        $(function () {
            $("[nameid='FormChange']").change(function () {
                $("#Form1").submit();
            });

        });
        function SetSearch(txt) {
            $("#SearchText").val(txt);
            $("#SearchText").change();
        }
    </script>
}
